package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.SysRoleAclMapper;
import com.example.demo.dao.SysRoleUserMapper;
import com.example.demo.exceptionhandler.BusinessException;
import com.example.demo.exceptionhandler.EmBusinessError;
import com.example.demo.model.SysAcl;
import com.example.demo.dao.SysAclMapper;
import com.example.demo.model.SysRoleAcl;
import com.example.demo.pageinfo.DoPage;
import com.example.demo.requesthandler.UserSession;
import com.example.demo.service.ISysAclService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.util.CodeGeneratorUtil;
import com.example.demo.util.IpAddressUtil;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2021-04-25
 */
@Service
public class SysAclServiceImpl extends ServiceImpl<SysAclMapper, SysAcl> implements ISysAclService {
    @Autowired
    private SysAclMapper sysAclMapper;
    @Autowired
    private SysRoleAclMapper sysRoleAclMapper;

    /**
     * 根据权限功能主键获取功能点列表
     *
     * @param aclModuleId
     * @return
     */
    @Override
    @DoPage
    public List<SysAcl> getListByAclModuleId(Integer aclModuleId) {
        QueryWrapper<SysAcl> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(true, "acl_module_id", aclModuleId)
                .orderByAsc("seq", "name");
        return sysAclMapper.selectList(queryWrapper);
    }

    /**
     * 添加某一条功能点信息
     *
     * @param sysAcl
     */
    @Override
    public int saveAclModule(SysAcl sysAcl) {
        if (checkExist(sysAcl.getAclModuleId(), sysAcl.getName(), sysAcl.getId())) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "当前权限模块下面存在相同名称的权限点");
        }
        sysAcl.setCode(CodeGeneratorUtil.generateCode());
        sysAcl.setOperator(UserSession.getLoginName());
        sysAcl.setOperateTime(new Date());
        sysAcl.setOperateIp(IpAddressUtil.getRemoteIp());
        return sysAclMapper.insert(sysAcl);
    }

    /**
     * 更新某一条功能点信息
     *
     * @param sysAcl
     */
    @Override
    public int updateAclModule(SysAcl sysAcl) {
        if (checkExist(sysAcl.getAclModuleId(), sysAcl.getName(), sysAcl.getId())) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "当前权限模块下面存在相同名称的权限点");
        }
        SysAcl before = sysAclMapper.selectById(sysAcl.getId());
        if (before == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "待更新的权限点不存在");
        }
        SysAcl after = new SysAcl();
        BeanUtils.copyProperties(sysAcl, after);
        after.setOperator(UserSession.getLoginName());
        after.setOperateTime(new Date());
        after.setOperateIp(IpAddressUtil.getRemoteIp());
        return sysAclMapper.updateById(after);
    }

    /**
     * 根据功能点id删除某一条功能点信息
     *
     * @param id
     */
    @Override
    public int deleteAclById(Integer id) {
        return sysAclMapper.deleteById(id);
    }

    /**
     * 根据角色id获取权限列表
     *
     * @param roleId
     * @return
     */
    @Override
    public List<SysAcl> getAclListByRoleId(Integer roleId) {
        List<Integer> aclIdList = sysRoleAclMapper.getAclIdListByRoleId(roleId);
        if (CollectionUtils.isEmpty(aclIdList)) {
            return Lists.newArrayList();
        }
        return sysAclMapper.getByIdList(aclIdList);
    }

    /**
     * 根据用户id获取权限列表
     *
     * @param userRoleIdList
     * @return
     */
    @Override
    public List<SysAcl> getUserAclList(List<Integer> userRoleIdList) {
        if (CollectionUtils.isEmpty(userRoleIdList)) {
            return Lists.newArrayList();
        }
        List<Integer> userAclIdList = sysRoleAclMapper.getAclIdListByRoleIdList(userRoleIdList);
        if (CollectionUtils.isEmpty(userAclIdList)) {
            return Lists.newArrayList();
        }
        return sysAclMapper.getByIdList(userAclIdList);
    }

    /**
     * 获取当前用户的权限列表
     *
     * @return
     */
    @Override
    public List<SysAcl> getCurrentUserAclList(List<Integer> userRoleIdList) {
        return getUserAclList(userRoleIdList);
    }

    /**
     * 代码块——用于检查DB中是否已经存在该记录
     *
     * @param aclModuleId
     * @param name
     * @param id
     * @return
     */
    public boolean checkExist(Integer aclModuleId, String name, Integer id) {
        QueryWrapper<SysAcl> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(id != null, "id", id)
                .eq(aclModuleId != null, "acl_module_id", aclModuleId)
                .eq(StringUtils.isNotBlank(name), "name", name);
        return sysAclMapper.selectCount(queryWrapper) > 0;
    }

}
